package cn.accp.kzy.dao.impl;

import cn.accp.kzy.dao.BBSuserDao;
import cn.accp.kzy.entity.BBSuser;
import java.sql.*;

public class BBSuserDaoImpl implements BBSuserDao {

	public boolean addUser(BBSuser user) {
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "";
		boolean flag = false;
		conn = BaseDao.getConnection();
		sql = "insert into BBSUsers values(?,?,?,?,?,?,default,?,getDate(),default,default)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, user.getUID());
			ps.setString(2, user.getUName());
			ps.setString(3, user.getUPassword());
			ps.setString(4, user.getUEmail());
			ps.setString(5, user.getUBirthday());
			ps.setInt(6, user.getUSex());
			ps.setString(7, user.getUStatement());
			int num = ps.executeUpdate();
			if(num > 0) {
				flag = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.close(conn, ps);
		}
		return flag;
	}

	public int getLastUID() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		int uid = 0;
		conn = BaseDao.getConnection();
		sql = "select * from BBSUsers order by UID desc";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next()){
				uid = rs.getInt("UID");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.close(conn, ps, rs);
		}
		return uid;
	}
	
}
